<%-- 
    Document   : index
    Created on : 12.04.2013, 23:06:53
    Author     : Yasya, Kate
--%>


<%@page import="entity.Users"%>
<%@page import="facade.CategoryFacadeLocal"%>
<%@page import="facade.ProductFacadeLocal"%>
<%@page import="java.util.List"%>
<%@page import="entity.Category"%>
<%@page import="entity.Product"%>
<%@page import="javax.naming.NamingException"%>
<%@page import="javax.naming.InitialContext"%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<%!    private ProductFacadeLocal productFacade;
    private CategoryFacadeLocal categoryFacade;

    public void jspInit() {
        try {
            InitialContext ic = new InitialContext();
            productFacade = (ProductFacadeLocal) ic.lookup(
                    "java:global/Delta/Delta-ejb/ProductFacade!facade.ProductFacadeLocal");
            categoryFacade = (CategoryFacadeLocal) ic.lookup(
                    "java:global/Delta/Delta-ejb/CategoryFacade!facade.CategoryFacadeLocal");

        } catch (NamingException ex) {
            ex.printStackTrace();
        }
    }
%>
<%
    Users user = (Users) session.getAttribute("user");
%>

<script>
    function getChecked(){
        var radios=document.getElementsByName("id_product");
        var id_product=-1;
        
        for(i=0;i<radios.length; i++){
            if(radios[i].checked==true){
                id_product=radios[i].value;
                }
        }
        return id_product;
    } 
    
    
    function deleteProduct(){
        
       var id_product=getChecked();
        if( id_product!=-1){
            var xmlhttp;
            xmlhttp=new XMLHttpRequest();
            xmlhttp.onreadystatechange=function()
            {
                if (xmlhttp.readyState==4 && xmlhttp.status==200)
                {
                    document.getElementById("productStatus_"+id_product).innerHTML="discontinued";
                
                }
            }
        
            xmlhttp.open("GET","DeleteProduct?id_product="+id_product
                ,true);
            xmlhttp.send();
            
        }else{
            alert("Please, choose a product to delete");
        }
            
    }
    

    function editProduct(){
        
        if( getChecked()!=-1){
            document.getElementById("mainForm").action="edit_product.jsp";
            var backURL;
            var name= document.getElementById("label").value;
            if(name==null){
                name="";
            }
       
            backURL="catalog_product.jsp?pageNumber="+currentPage
                +"&name="+name
                +"&category="+document.getElementById("category").value
                +"&sortBy="+document.getElementById("sortBy").value;
            document.getElementById("backURL").value=backURL;
            document.getElementById("mainForm").submit();
        }else{
            alert("Please, choose a product to edit");
        }
        
    }
</script>

<script>
    function cleanInput(){
        document.getElementById("label").value="";
        doFilter();
    }    
    
    function selectCategory(){
        document.getElementById("selectForm").submit();
    }    
</script>
<script>
    function getProducts(pageNumber){
       
        var xmlhttp;
        xmlhttp=new XMLHttpRequest();
        xmlhttp.onreadystatechange=function()
        {
            if (xmlhttp.readyState==4 && xmlhttp.status==200)
            {
                document.getElementById("products").innerHTML=xmlhttp.responseText;
                printPages(pageNumber);
                
            }
        }
        var name=document.getElementById("label").value;
        var category=document.getElementById("category").value;
        var sortBy=document.getElementById("sortBy").value;
        
        xmlhttp.open("POST",encodeURI("products.jsp?pageNumber="+pageNumber+
            "&sortBy="+sortBy+
            "&category="+category+
            "&name="+name)
        ,true);
        xmlhttp.send();
    } 
    
</script>
<% int pageNumber;
    String pageNum;
    String name;
    String id_category_p;
    int id_category;
    String sortBy;
    int sortField;


//parse pageNumber
    pageNum = request.getParameter("pageNumber");
    try {
        pageNumber = Integer.parseInt(pageNum);
    } catch (NumberFormatException ex) {
        pageNumber = 1;
    }

//parse category        
    name = request.getParameter("name");
    if (name == null) {
        name = "";
    }

//parse category
    id_category_p = request.getParameter("category");
    try {
        id_category = Integer.parseInt(id_category_p);
    } catch (NumberFormatException ex) {
        id_category = -1;
    }

//parse sortBy
    String sortBy_p = request.getParameter("sortBy");
    try {
        sortField = Integer.parseInt(sortBy_p);
    } catch (NumberFormatException ex) {
        sortField = 4;
    }

%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link rel="stylesheet" type="text/css" href="css/styl.css">
              <link rel="stylesheet" type="text/css" href="css/all.css">
        <script type="text/javascript" src="scripts/paging.js"></script>

        <title>Work with product</title>
    </head>
    <body onload="changePage(1)">
        <header>
            <div id ="top_name"> <a href="main.jsp"> 
                    <img src="/Delta-war/product/1.jpeg"></a><h1> Warehouse employee</h1> </div>
           
            <div id="registr2">
                <p> <a href="user_name.jsp"><%=user.getLogin()%></a>
                    <a href="logout">LogOut</a> </p>
            </div>
            <div id="menu">
                <div id="line"> <a href="WarehouseOrders"> Orders</a> </div>
                <div id="line"> <a href="ViewWarehouse"> Warehouses</a> </div>
                <div id="line" style="background: #ff0000; "> <a href="catalog_product.jsp"> Product</a> </div>
            </div>

            <div id="user-panel">
                <%@ include file="rolefragment.jsp" %>        </div>
        </div>
    </header>
    <div id="main">
        <div id="find">  
            <form method="POST" action="catalog_product.jsp"> 
                <p>Find by name:
                    <input 
                        type="text" 
                        name="Find" 
                        size="30" 
                        value="<%=name%>" 
                        id="label"
                        placeholder="Enter product name to search" />
                    <input 
                        id="go"  
                        type="button"   
                        name="go"
                        value="Go"
                        onclick="doFilter()"
                        /> 
                    <input 
                        id="clean_btn"  
                        type="button" 
                        style="background: #b8b0fd; width: 60px;"
                        name="clean"
                        value="Clean"
                        onclick="cleanInput()"/> 
                </p>

            </form>
        </div>
        <div id="generate"> 
            <form method="POST" action="gen_report_on_SOs.jsp">   
                <button 
                    type="submit" 
                    name="export">
                    Generate statistic report on orders
                </button>
            </form>
        </div>  

        <div id="generatePopular"> 
            <form method="POST" action="popularProductsReport.jsp">   
                <button 
                    type="submit" 
                    name="export" 
                    >
                    Generate report  popular product
                </button>
            </form>
        </div>
        <div id="generateOnAllOrders"> 
            <!--<form method="POST" action="gen_report_on_AllOrders.jsp">-->
            <form method="POST" action="general_report.jsp">
                <button 
                    type="submit" 
                    name="export" 
                    >
                    Generate report on all orders
                </button>
            </form>
        </div>
        <div id="sorted">
            <form method="GET" id="selectForm" action="catalog_product.jsp">
                <p> Category: 
                    <select  
                        id="category" 
                        name="category" 
                        size = 1
                        onchange="doFilter()">
                        <option 
                            value="-1">all
                        </option>

                        <%for (Category c : categoryFacade.findAll()) {%>
                        <option 
                            value=<%=c.getId_category()%>  
                            <%if (id_category == c.getId_category()) {
                                    out.print("selected");
                                }%>>
                            <%= c.getName()%> 
                        </option> 
                        <%}%>

                    </select> 
                    <a href="change_category.jsp"> 
                        Create/edit category 
                    </a>
                </p>
            </form>
            <form method="GET" id="selectSort" action="catalog_product.jsp">
                Sort by:   
                <select 
                    id="sortBy"
                    name="sortBy"  
                    size = 1 
                    onchange="doFilter()"
                    style="width:100"> 
                    <option value="1" <%if (sortField == 1) {
                            out.print("selected");
                        }%>>price asc</option>
                    <option value="2" <%if (sortField == 2) {
                            out.print("selected");
                        }%>>price desc</option>
                    <option value="3" <%if (sortField == 3) {
                            out.print("selected");
                        }%>>name desc</option>
                    <option value="4" <%if (sortField == 4) {
                            out.print("selected");
                        }%>>name asc</option>            
                </select> 
            </form>
        </div>
        <v id="tabl1">
            <div id="top_pages">
            </div>
            <form id="mainForm">
                <input
                    id="backURL"
                    name="backURL"
                    type="hidden"
                    value=""
                    />
                Products
                <div id="products">

                </div>

            </form>
    
    <div id="line_button" style="bottom: 0px; z-index:1"> 

        <form method="POST" action="add_product.jsp">
            <button
                id="function" 
                type="submit" 
                >
                Add product
            </button>
        </form>
    </div>



    <div id="line_button" style="bottom: 0px; z-index:1"> 
        <button 
            id="function" 
            onclick="editProduct()">
            Edit product
        </button></div>

    <div id="line_button" style="bottom: 0px; z-index:1"> 
        <button 
            id="function" 
            onclick="deleteProduct()">
            Delete product
        </button> </div>


    <div id="bottom_pages" style="clear: left">
    </div>
</div>
<div id="footer">
                                <p>  &#169;All rights reserved</p>
                            </div>

</body>
</html>
